package com.lagou.controller;

import com.lagou.service.CodeService;
import com.lagou.service.EmailService;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/code")
public class CodeController {

    @Autowired
    private CodeService codeService;
    @Reference
    private EmailService emailService;

    /**
     * ⽣成验证码并发送到对应邮箱，成功true，失败 false
     * @param email
     * @return
     */
    @PostMapping("/create/{email}")
    public Boolean createCode(@PathVariable String email) {

        String code = RandomStringUtils.randomAlphanumeric(6);

        codeService.addCode(email, code);

        Boolean result = emailService.sendEmail(email, code);

        return result;
    }

}
